using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_threads_ThreadPosts : Controls.DataManagerThreadControl
{
    public void SetDataSource(List<DAL.Post> posts)
    {
        gridPosts.DataSource = posts;
        gridPosts.DataBind();
    }

    // I'm creating this temp variable here because the DAL.Users.GetCurrentUsers() function
    // will attempt to query the database if the user object is null. Since I need to know the
    // current user to determine whether a user can edit a post, this function is called once per
    // post; if a user isn't logged into an account, I don't waste time querying the database.
    private bool userRetrieved = false;
    private DAL.User _currentUser;
    protected DAL.User CurrentUser
    {
        get
        {
            if (!userRetrieved)
            {
                CurrentUser = Managers.UserLoginManager.CurrentUser;
                userRetrieved = true;
            }
            return _currentUser;
        }
        set
        {
            _currentUser = value;
        }
    }
    
    protected bool IsUserLoggedIn
    {
        get
        {
            return CurrentUser != null;
        }
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    private void Refresh()
    {
        SetDataSource(DAL.Posts.GetByThread(CurrentThread));
    }

    protected string GetSignature(DAL.User user)
    {
        if (user.AdminSettings.AllowSignature &&
            user.HasSignature &&
            (CurrentUser == null || CurrentUser.UserSettings.ShowUserSignatures))
            return string.Format("<p>__________<br />{0}</p>", DAL.YCodes.Format(user.Signature));
        else
        {
            return "";
        }
    }

    protected string GetUserTitle(DAL.User user)
    {
        if (user.AdminSettings.AllowUserTitle)
            return Helpers.StringHelper.FormatIfExists("<div class=\"forum_grid_item_description\">{0}</div>", user.UserTitle);
        else
        {
            return "";
        }
    }

    protected bool CanEditPost(DAL.User user)
    {
        if (CurrentUser != null && CurrentUser.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            return true;
        }
        else
        {
            if (CurrentUser != null)
            {
                return user == CurrentUser;
            }
            else
            {
                return false;
            }
        }
    }

    protected string GetAvatar(DAL.User user)
    {
        if (user.AdminSettings.AllowAvatar && 
            user.HasAvatar && 
            (CurrentUser == null || CurrentUser.UserSettings.ShowUserAvatars))
            return Helpers.StringHelper.FormatIfExists("<div><img src=\"avatars/{0}.{1}\" /></div>", user.ID, user.AvatarExtension);
        else
        {
            return "";
        }
    }
}
